package com.csj.mvvm.utils

import android.app.Activity
import android.app.Application
import android.app.Application.ActivityLifecycleCallbacks
import android.os.Bundle


class BaseLifecycleCallback: ActivityLifecycleCallbacks {

    /**
     * 单一实例
     */
    companion object {
        private var instance: BaseLifecycleCallback? = null
        fun getInstance() = instance ?: BaseLifecycleCallback().also { instance = it }
    }

    /**
     * 必须在 Application 的 onCreate 方法中调用
     */
    fun init(application: Application) {
        application.registerActivityLifecycleCallbacks(this)
    }

    override fun onActivityCreated(
        activity: Activity,
        savedInstanceState: Bundle?
    ) {
//        LogUtil.e("activity 生命周期", activity.getClass().getSimpleName() + " --> onActivityCreated");
        AppManager.instance.addActivity(activity)
    }

    override fun onActivityStarted(activity: Activity) {
//        LogUtil.e("activity 生命周期", activity.getClass().getSimpleName() + " --> onActivityStarted");
    }

    override fun onActivityResumed(activity: Activity) {
//        LogUtil.e("activity 生命周期", activity.getClass().getSimpleName() + " --> onActivityResumed");
    }

    override fun onActivityPaused(activity: Activity) {
//        LogUtil.e("activity 生命周期", activity.getClass().getSimpleName() + " --> onActivityPaused");
    }

    override fun onActivityStopped(activity: Activity) {
//        LogUtil.e("activity 生命周期", activity.getClass().getSimpleName() + " --> onActivityStopped");
    }

    override fun onActivitySaveInstanceState(
        activity: Activity,
        outState: Bundle
    ) {
//        LogUtil.e("activity 生命周期", activity.getClass().getSimpleName() + " --> onActivitySaveInstanceState");
    }

    override fun onActivityDestroyed(activity: Activity) {
//        LogUtil.e("activity 生命周期", activity.getClass().getSimpleName() + " --> onActivityDestroyed");
        AppManager.instance.finishActivity(activity)
    }

}